package reittiopas;

/**
*
* @author Jaakko, Petteri
*/
public class TimeFormatter implements ITimeFormatter {

	
	public TimeFormatter() {
		
	}
	
	/**
	 * Parses a time string for route service, e.g.:
	 * 13:12 --> 1312
	 * 3:56 --> 0356
	 * 7:8 --> 0708
	 */
	
public  String trimTimeString(String rawString) {
		
		if(rawString.length() == 0) {
			throw new RuntimeException("Incorrect time format");
		}
	
		if(rawString.length() > 5) {
			throw new RuntimeException("Incorrect time format");
		}
		if (rawString.length() == 4 && rawString.indexOf(':') < 0)
			return rawString;
	
//		String validString = "";
//		for(int i = 0; i<rawString.length(); i++) {
//			if(rawString.charAt(i) != ':') {
//				validString += rawString.charAt(i);
//			}
//		}
		
		String hours = rawString.substring(0, rawString.indexOf(':'));
		if (hours.length() == 1)
			hours = "0" + hours;
		String minutes = rawString.substring(rawString.indexOf(':')+1, rawString.length());
		if (minutes.length() == 1)
			minutes = "0" + minutes;
		
		
		return "" + hours + minutes;
		
		
	}
	
	
}
